// pages/manage/customer/exchange/index.js
import {http} from "../../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    number: 0,
    object: "",
    isRemark: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: JSON.parse(decodeURIComponent(options.user))
    })
  },

  /** 积分输入框 */
  numInput(e){
    this.setData({
      number: e.detail.value
    })
  },

  /** 兑换物品输入框 */
  objectInput(e){
    this.setData({
      object: e.detail.value
    })
  },

  /** 备注选择 */
  radioChange(e){
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    if(e.detail.value=="yes"){
      this.data.isRemark = true;
      this.data.object = "";
    }else{
      this.data.isRemark = false;
      this.data.object = "无";
    }
    this.setData({
      isRemark: this.data.isRemark,
      object: this.data.object
    })
  },

  /** 立即兑换 */
  exchangeSubmit(){
    if(!this.data.number){
      return http.showToast("请输入消耗积分数")
    }
    if(!this.data.object){
      return http.showToast("请输入兑换物品")
    }
    if(this.data.number > this.data.userInfo.IntegralNum){
      return http.showToast("积分不足")
    }
    wx.showModal({
      title: '提示',
      content: '一旦兑换无法撤销，是否确定兑换',
      complete: (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          this.confirmSubmit();
        }
      }
    })
    
  },

  confirmSubmit(){
    let that = this;
    http.requestData("POST","PointExchange/PointExchange",{
      MemberId: this.data.userInfo.MemberId, //用户id,
      Integral: this.data.number, //积分
      ExName: this.data.object, //物品名称
      IsRemark: this.data.isRemark, // true 是我要备注 false 我不要
      Remark: "", //备注
    }).then(res=>{
      if(res.Tag == 10){
        http.showToast("兑换成功")
        let intNum = that.data.userInfo.IntegralNum;
        that.data.userInfo.IntegralNum = intNum - that.data.number;
        that.setData({
          userInfo: that.data.userInfo
        })
      }else{
        http.showToast(res.Message)
      }
    }).catch(e=>{
      http.showToast("网络服务繁忙")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})